package org.oschina.api.favorite.favorite.service;

import java.util.List;

import org.oschina.Config;
import org.oschina.Constants;
import org.oschina.Message;
import org.oschina.ResultUtil;
import org.oschina.api.BaseService;
import org.oschina.api.favorite.favorite.dao.FavoriteDAO;
import org.oschina.api.favorite.favorite.model.Favorite;
import org.oschina.api.favorite.favorite.vo.FavoriteVo;
import org.oschina.api.favorite.type.service.FavoriteTypeService;
import org.oschina.api.index.service.IndexService;
import org.oschina.api.manage.config.service.ConfigService;
import org.oschina.api.user.user.model.User;
import org.oschina.api.user.user.service.UserService;
import org.oschina.bean.Pager;
import org.oschina.bean.Result;
import org.oschina.security.annotation.Require;
import org.oschina.util.DateUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class FavoriteService extends BaseService {

	@Autowired
	private FavoriteDAO favoriteDAO;
	@Autowired
	private UserService userService;
	@Autowired
	private FavoriteTypeService favoriteTypeService;
	@Autowired
	private ConfigService configService;
	@Autowired
	private IndexService indexService;
	
	/**
	 * 根据用户和类型返回
	 * @param type
	 * @param pn
	 * @return
	 */
	@Require
	public Pager<FavoriteVo> findByType(int type,int pn)
	{
		if(favoriteTypeService._findVoById(type)==null)
			return null;
		
		User user=userService.getAuthenticationInfo(getToken());
		Pager<FavoriteVo> pager=new Pager(pn,_getCountByUserAndType(user.getId(), type),_getPageCount(),null);
		if(pager.getData_sum()>0)
			pager.setData(_findVoByUserAndType(user.getId(),type,pager.getStart(),pager.getPage_count()));
		return pager;
	}
	
	/**
	 * 创建
	 * @param user
	 * @param major
	 * @param type
	 * @return
	 */
	@Require
	public Result create(int user,String title,int major,int type)
	{
		if(_isFavorite(user, major, type))
			return ResultUtil.getFailure(Message.FAVORITE_CREATE_FAILURE_IS_FAVORITE);
		
		Favorite favorite=new Favorite();
		favorite.setUser(user);
		favorite.setTitle(title);
		favorite.setMajor(major);
		favorite.setType(type);
		_create(favorite);
		return ResultUtil.getSuccess(favorite);
	}
	
	/**
	 * 删除
	 * @param user
	 * @param major
	 * @param type
	 * @return
	 */
	@Require
	public Result delete(int user,int major,int type)
	{
		if(_deleteByUserAndMajorAndType(user, major, type)==0)
			return ResultUtil.getFailure(Message.ERROR_DATA_ERROR);
		
		return ResultUtil.getSuccess(Message.FAVORITE_DELETE_SUCCESS);
	}
	
	/**
	 * 是否收藏
	 * @param user
	 * @param major
	 * @param type
	 * @return
	 */
	public boolean _isFavorite(int user,int major,int type)
	{
		return favoriteDAO.isFavorite(user, major, type);
	}
	
	/**
	 * 根据用户和主键和类型删除
	 * @param user
	 * @param major
	 * @param type
	 * @return
	 */
	private int _deleteByUserAndMajorAndType(int user,int major,int type)
	{
		return favoriteDAO.deleteByUserAndMajorAndType(user, major, type);
	}
	
	/**
	 * 根据ID返回
	 * @param id
	 * @return
	 */
	private FavoriteVo _findVoById(int id)
	{
		return favoriteDAO.findVoById(id);
	}
	
	/**
	 * 根据用户和类型返回数量
	 * @param user
	 * @param type
	 * @return
	 */
	private int _getCountByUserAndType(int user,int type)
	{
		return favoriteDAO.getCountByUserAndType(user, type);
	}
	
	/**
	 * 根据用户和类型返回
	 * @param user
	 * @param type
	 * @param first
	 * @param max
	 * @return
	 */
	private List<FavoriteVo> _findVoByUserAndType(int user,int type,int first,int max)
	{
		return favoriteDAO.findVoByUserAndType(user, type, first, max);
	}
	
	/**
	 * 创建
	 * @param favorite
	 * @return
	 */
	private Favorite _create(Favorite favorite)
	{
		favorite.setId(_getIndex());
		favorite.setCreate_time(DateUtil.now());
		return favoriteDAO.create(favorite);
	}
	
	private int _getPageCount()
	{
		return (Integer)configService.getValueById(Config.CONFIG_21, Integer.class);
	}
	
	private int _getIndex()
	{
		return indexService.getMajorById(Constants.INDEX_FAVORITE);
	}
}